/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.excel;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.modules.classes.entity.ClassesUserEntity;
import org.springblade.modules.classes.excel.ClassesUserExcel;
import org.springblade.modules.classes.service.IClassesUserService;
import org.springblade.modules.material.entity.ReleaseExamUserEntity;
import org.springblade.modules.material.service.IReleaseExamUserService;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 考试学员数据导入类
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class ReleaseExamUserImporter implements ExcelImporter<ReleaseExamUserExcel> {

	private final IReleaseExamUserService releaseExamUserService;
	private final IUserBaseService userBaseService;
	private final Long releaseId;

	@Override
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public void save(List<ReleaseExamUserExcel> dataList) {
		if (dataList.size()>1000)throw new ServiceException("导入条数不可超过1000条");
		String version= DateUtil.format(new Date(),"yyyyMMddHHmmss");

		List<ReleaseExamUserEntity> reusltList=new ArrayList<>();
		List<ReleaseExamUserEntity> oldList=releaseExamUserService.list(Wrappers.<ReleaseExamUserEntity>query().lambda().eq(ReleaseExamUserEntity::getReleaseId,releaseId));

		for (ReleaseExamUserExcel data:dataList){
			UserBaseEntity userBaseEntity=userBaseService.getOne(Wrappers.<UserBaseEntity>query().lambda().eq(UserBaseEntity::getUserNumber,data.getUserNumber()));
			//userBase不能为空，且没有录入过
			if (null!=userBaseEntity && !isExist(oldList,userBaseEntity.getUserId()) ){
				ReleaseExamUserEntity entity=new ReleaseExamUserEntity();
				entity.setVersion(version);
				entity.setReleaseId(releaseId);
				entity.setUserId(userBaseEntity.getUserId());
				reusltList.add(entity);
			}
		}

		//批量保存
		releaseExamUserService.saveBatch(reusltList);
	}

	/**
	 *  判断是否已经录入
	 *  已录入 true
	 *  未录入 false
	 */
	public boolean isExist(List<ReleaseExamUserEntity> list,Long userId){
		for (ReleaseExamUserEntity entity:list){
			if (entity.getUserId().intValue()==userId.intValue()) {
				return true;
			}
		}
		return false;
	}


}
